package com.orange.adminapi.controller.banner;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.service.banner.BannerService;
import com.orange.core.pojo.BR;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.banner.dto.BannerDTO;
import com.orange.core.pojo.entity.banner.vo.BannerVO;
import com.orange.core.pojo.req.banner.BannerPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>ClassName:BannerController</p>
 * <p>Description: banner管理Controller</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/6/2 23:35</p>
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/banner")
public class BannerController {

    private final BannerService bannerService;

    /**
     * banner管理分页查询
     * @param pageReqDto
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).BANNER_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<BannerVO>> page(@RequestBody PageReqDto<BannerPageReq> pageReqDto){
        Page<BannerVO> page = bannerService.page(pageReqDto);
        return R.ok(page);
    }

    /**
     * banner管理详情
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).BANNER_INFO.permission.identifier)")
    @GetMapping("/info/{id}")
    public R<BannerVO> info(@PathVariable("id")Integer id){
        return R.ok(bannerService.info(id));
    }

    /**
     * banner管理新增
     * @param bannerDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).BANNER_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated BannerDTO bannerDTO){
        return BR.result(bannerService.save(bannerDTO));
    }

    /**
     * banner管理修改
     * @param bannerDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).BANNER_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated BannerDTO bannerDTO){
        return BR.result(bannerService.updateById(bannerDTO));
    }

    /**
     * banner管理删除
     * @param ids
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).BANNER_DELETE.permission.identifier)")
    @DeleteMapping("/{ids}")
    public R<Boolean> delete(@PathVariable("ids") Integer[] ids){
        return BR.result(bannerService.deleteBannerByIds(ids));
    }
}
